package cn.com.greatwall.system.modules.system.rest;

import java.util.Objects;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.common.utils.enums.CodeBiEnum;
import cn.com.greatwall.common.utils.enums.CodeEnum;
import cn.com.greatwall.system.modules.system.service.VerifyService;
import cn.com.greatwall.tools.domain.vo.EmailVo;
import cn.com.greatwall.tools.service.EmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2020-07-23 4:43:14 PM
 * @Version 1.0
 * @Description:
 */
@Api(tags = "系统：验证码管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/code")
public class VerifyController {
    private final VerifyService verifyService;
    private final EmailService emailService;

    @PostMapping(value = "/resetEmail")
    @ApiOperation("重置邮箱，发送验证码")
    public ResponseEntity<Object> resetEmail(@RequestParam String email) {
        EmailVo emailVo = verifyService.sendEmail(email, CodeEnum.EMAIL_RESET_EMAIL_CODE.getKey());
        emailService.send(emailVo, emailService.find());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/email/resetPass")
    @ApiOperation("重置密码，发送验证码")
    public ResponseEntity<Object> resetPass(@RequestParam String email) {
        EmailVo emailVo = verifyService.sendEmail(email, CodeEnum.EMAIL_RESET_PWD_CODE.getKey());
        emailService.send(emailVo, emailService.find());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/validated")
    @ApiOperation("验证码验证")
    public ResponseEntity<Object> validated(@RequestParam String email, @RequestParam String code,
            @RequestParam Integer codeBi) {
        CodeBiEnum biEnum = CodeBiEnum.find(codeBi);
        switch (Objects.requireNonNull(biEnum)) {
        case ONE:
            verifyService.validated(CodeEnum.EMAIL_RESET_EMAIL_CODE.getKey() + email, code);
            break;
        case TWO:
            verifyService.validated(CodeEnum.EMAIL_RESET_PWD_CODE.getKey() + email, code);
            break;
        default:
            break;
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
